USE [bmig_Mensual_Post_cadena]
GO

IF EXISTS (SELECT name FROM   sysobjects WHERE  name = 'WASP_M0011_CARGATBL_CodSegSalud' AND type = 'P')
    DROP PROCEDURE [dbo].[WASP_M0011_CARGATBL_CodSegSalud]
GO

CREATE PROCEDURE [dbo].[WASP_M0011_CARGATBL_CodSegSalud]
(
	@FECHA_INICIO     [char](8),
	@FECHA_TERMINO    [char](8)
)

AS

BEGIN

		INSERT INTO codsegSalud

		SELECT 
			   fsd611.ppsuc,   fsd611.ppcta  , fsd611.ppoper,
			   fsd611.ppimp11, fsd611.ppimp12, fsd611.ppimp13, 
			   fsd611.ppimp14, fsd611.ppimp15, fsd611.ppimp16,
			   fsd611.ppimp17, fsd611.ppimp18, fsd611.ppimp19

		FROM 
				fsd611  (nolock), 
				jt73109 (nolock), 
				fsd601  (nolock)

		WHERE  
				jt73109.jt73109fpa   = fsd611.ppoper and
				jt73109.jt73109suc  = fsd611.ppsuc and
				jt73109.jt73109FAP between @FECHA_INICIO and @FECHA_TERMINO and
				fsd611.pgcod  = fsd601.pgcod and
				fsd611.ppmod  = fsd601.ppmod and
				fsd611.ppsuc  = fsd601.ppsuc and 
				fsd611.ppmda  = fsd601.ppmda and
				fsd611.ppcta  = fsd601.ppcta and 
				fsd611.ppoper = fsd601.ppoper and
				fsd611.ppsbop = fsd601.ppsbop and
				fsd611.pptope = fsd601.pptope and
				fsd601.d601co = 'S' and 
				fsd611.pptipo = '' and
				fsd611.ppexte = 9 
		
		GROUP BY    fsd611.ppsuc,   fsd611.ppcta, fsd611.ppoper,
					fsd611.ppimp11, fsd611.ppimp12, fsd611.ppimp13, 
					fsd611.ppimp14, fsd611.ppimp15, fsd611.ppimp16,
					fsd611.ppimp17, fsd611.ppimp18, fsd611.ppimp19
		          
END